package cc.vv.lkbasecomponent.base.ui.loading;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;


/**
 * 基类loading层.传入自定义的loading层的内容layout.
 * @author dupeng dupeng@vv.cc
 * @date 2018/3/7
 */

public class LKBaseLoading implements InterLoading {

    protected LoadingCoverView coverView;

    protected final int mContentLayoutId;

    protected View itemView;

    public LKBaseLoading(int contentView) {
        mContentLayoutId = contentView;
    }

    @Override
    public void showLoading() {
        if (coverView != null) {
            coverView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void closeLoading() {
        if (coverView != null) {
            coverView.setVisibility(View.GONE);
        }
    }

    @Override
    public void initLoadingView(Activity activity) {
        if (activity == null) {
            return;
        }
        coverView = new LoadingCoverView(activity);

        itemView = LayoutInflater.from(activity).inflate(mContentLayoutId, null);
        coverView.addView(itemView, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        decorView.addView(coverView, params);
        coverView.setVisibility(View.GONE);
    }

    @Override
    public boolean isShowing() {
        if (coverView == null) {
            return false;
        }
        if (coverView.getVisibility() == View.VISIBLE) {
            return true;
        }
        return false;
    }

    @Override
    public void setCanPierce(boolean canPierce) {
        if (coverView != null) {
            coverView.setCanPierce(canPierce);
        }
    }

    @Override
    public void addPierceViews(View[] views) {
        if (coverView != null) {
            coverView.addCanPierceViews(views);
        }
    }
}
